<?php
require_once 'lib/Service.php';
require_once 'lib/IdWork.php';
require_once 'lib/CException.php';

class IDCountService extends Service{

	private $defaultCount = 10000;
	
	public function __construct(){
		parent::__construct( );
		$this->_datakey = $this->_config['redisDatabase']['idkey'];
	}
	
	/*
	 * 全局id
	 *@return string
	 */
	public function generateId(){
		//return IdWork::generateId( $this->idcount() );
		return $this->idcount();
	}

	public function idcount(){
		$currentId = $this->defaultCount;
		$this->_redis->select( $this->_datakey );
		if( $this->_redis->exists( $this->_config['redisKey']['userCount'] ) ){
			$currentId = $this->_redis->incr( $this->_config['redisKey']['userCount'] );
		}else{
			$currentId = $this->_redis->incrby( $this->_config['redisKey']['userCount'], $currentId );
		}

		if( $currentId < $this->defaultCount){
			throw new CException("count error", 530);
		}
		return $currentId;
	}
}
